package com.xunji.user.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunji.user.common.R;
import com.xunji.user.dto.RoleMenuDto;
import com.xunji.user.entity.Role;
import com.xunji.user.service.RoleService;
import com.xunji.user.vo.req.RoleAddRequest;
import com.xunji.user.vo.req.RoleQueryRequest;
import com.xunji.user.vo.req.RoleUpdateRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Resource
    RoleService roleService;


    @PostMapping("/page")
    @ApiOperation(value = "查询角色", notes = "查询角色")
    public R getRolesPage(@Validated @RequestBody RoleQueryRequest request) {
        try {
            Page<RoleMenuDto> roleMenuDtoPage = roleService.page(request);
            return R.success(roleMenuDtoPage);
        } catch (Exception e) {
            log.error("查询角色失败，e->{}", e);
            return R.failure("查询失败");
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有角色", notes = "查询所有角色")
    public R getRoles() {
        try {
            List<Role> roles = roleService.getRoles();
            return R.success(roles);
        } catch (Exception e) {
            log.error("查询所有角色，e->{}", e);
            return R.failure("查询失败");
        }
    }

    @PutMapping("")
    @ApiOperation(value = "新增角色", notes = "新增角色")
    public R addRole(@RequestBody RoleAddRequest request) {
        // 校验重名
        if (!StringUtils.hasText(request.getName())) {
            return R.failure("角色名不能为空");
        }
        List<Role> roles = roleService.selectByName(request.getName());
        if (roles != null && roles.size() > 0) return R.failure("角色名重复");
        try {
            roleService.add(request);
            return R.success();
        } catch (Exception e) {
            log.error("新增角色失败，e->{}", e);
            return R.failure("新增角色失败");
        }
    }

    @PostMapping("")
    @ApiOperation(value = "更新角色", notes = "更新角色")
    public R updateRole(@RequestBody RoleUpdateRequest request) {
        // 校验重名
        if (!StringUtils.hasText(request.getName())) {
            return R.failure("角色名不能为空");
        }
        List<Role> roles = roleService.selectByNameWithoutId(request.getName(), request.getId());
        if (roles != null && roles.size() > 0) return R.failure("角色名重复");
        try {
            roleService.update(request);
            return R.success();
        } catch (Exception e) {
            log.error("更新角色失败，e->{}", e);
            return R.failure("更新角色失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    public R deleteRole(@PathVariable("id") String id) {
        try {
            roleService.deleteById(id);
            return R.success();
        } catch (Exception e) {
            log.error("删除角色失败，e->{}", e);
            return R.failure("删除角色失败");
        }
    }

    @DeleteMapping("")
    @ApiOperation(value = "批量删除角色", notes = "批量删除角色")
    public R deleteRoles(@RequestParam List<String> ids) {
        try {
            ids.stream().forEach(id -> {
                roleService.deleteById(id);
            });
            return R.success();
        } catch (Exception e) {
            log.error("删除角色失败，e->{}", e);
            return R.failure("删除角色失败");
        }
    }
}
